package com.example.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import java.nio.charset.StandardCharsets;

public class ControlFragment extends Fragment implements View.OnClickListener {

    ImageButton imageButton_send;
    TextView textView_ctrl_rec;
    EditText editText_inputdata;
    ImageView imageView_led;
    Switch switch_led;
    public ControlFragment.cmdReceiver cmdReceiver;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View ControlFragment=inflater.inflate(R.layout.fragment_control, container, false);

        imageButton_send=ControlFragment.findViewById(R.id.imageButton_send);
        imageButton_send.setOnClickListener(this);

        textView_ctrl_rec=ControlFragment.findViewById(R.id.textView_ctrl_rec);
        editText_inputdata=ControlFragment.findViewById(R.id.editText_inputdata);
        imageView_led=ControlFragment.findViewById(R.id.imageView_led);
        switch_led=ControlFragment.findViewById(R.id.switch_led);
        switch_led.setOnClickListener(this);
        //注册广播接收器
        cmdReceiver =new ControlFragment.cmdReceiver();
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction("com.example.communication.data");
        getActivity().registerReceiver(cmdReceiver,intentFilter);

        return ControlFragment;
    }

    @Override
    public void onClick(View view) {
        switch(view.getId())
        {
            case R.id.imageButton_send:
            {
                String inputdataStr=editText_inputdata.getText().toString();
                byte[] inputdataByteArr = inputdataStr.getBytes(StandardCharsets.UTF_8);
                TCPService.send(inputdataByteArr);
            }break;
            case R.id.switch_led:
            {
                if(switch_led.isChecked())//打开
                {
                    String ledonStr="LEDON";
                    byte[] ledonByte = ledonStr.getBytes(StandardCharsets.UTF_8);
                    TCPService.send(ledonByte);
                    imageView_led.setImageDrawable(getResources().getDrawable(R.drawable.lighton));
                }
                else
                {
                    String ledoffStr="LEDOFF";
                    byte[] ledoffByte = ledoffStr.getBytes(StandardCharsets.UTF_8);
                    TCPService.send(ledoffByte);
                    imageView_led.setImageDrawable(getResources().getDrawable(R.drawable.lightoff));
                }
            }break;
        }
    }

    private class cmdReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            String recieveData=intent.getStringExtra("data");
            if(recieveData!=null)
            {
                textView_ctrl_rec.setText("接收到数据:"+recieveData);
            }
        }
    }
}